package com.fan.shop.dao;

import java.util.List;

import javax.inject.Named;

import org.hibernate.SessionFactory;

import com.fan.shop.domain.Item;

@Named("itemDao")
public class ItemDaoImpl implements ItemDao{

	
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Item getItem(String itemcode) {
		Item item = (Item)sessionFactory.getCurrentSession().get(Item.class, itemcode);
		return item;
	}

	
	@SuppressWarnings("unchecked")
	public List<Item> getAllItems() {
		List<Item> list = sessionFactory.getCurrentSession().createCriteria(Item.class).list();
		return list;
	}

	public int getSize() {
		return getAllItems().size();
	}

	public void addItem(Item item) {
		sessionFactory.getCurrentSession().save(item);
		
	}

	public void updateItem(Item item) {
		sessionFactory.getCurrentSession().update(item);
		
	}

	public void deleteItem(Item item) {
		sessionFactory.getCurrentSession().delete(item);
		
	}

	public boolean hasItem(String itemcode) {
		if(getItem(itemcode) == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> getColumnNames() {
		List<String> columns = sessionFactory
				.getCurrentSession()
				.createSQLQuery(
						"SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ITEM'")
				.list();

		return columns;
	}


}
